package connector;

import container.Server;
import container.Service;
import lifecycle.LifecycleBase;

import java.io.IOException;

/**
 * @ClassName : Connector
 * @Description : Connector用于接受请求并将请求封装成Request和Response，
 * 然后交给Container进行处理，Container处理完之后在交给Connector返回给客户端。
 * @Author : zhangxiaohai
 * @Date: 2021-06-24 08:17
 */
public class Connector extends LifecycleBase {

    //端口
    private Integer port;
    private Service service;
    private Server server;
    private ProtocolHandler protocolHandler;

    public Connector(ProtocolHandler protocolHandler) {
        this.protocolHandler = protocolHandler;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public void setServer(Server server) {
        this.server = server;
    }

    public Server getServer() {
        return this.server;
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    @Override
    protected void initInternal() {
        try {
            protocolHandler.init();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void startInternal() {
        try {
            protocolHandler.setExecutor(server.getExecutor());
            protocolHandler.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
